package com.weibo.user.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.weibo.common.CommonUtil;
import com.weibo.common.DB;
import com.weibo.sql.SqlFile;
import com.weibo.user.Billing;
import com.weibo.user.User;
import com.weibo.user.dao.UserDao;

public class UserDaoImp implements UserDao{
	private static Logger logger =  Logger.getLogger(UserDaoImp.class);
	@Override
	public boolean deleteUser_byID(String user_id) {
		// TODO Auto-generated method stub
		return DB.sqlNonExeQuery(CommonUtil.getSqlBy_par(SqlFile.USER_DELETE_BYID, user_id));
	}

	@Override
	public boolean deleteUser_byName(String user_name) {
		// TODO Auto-generated method stub
		return DB.sqlNonExeQuery(CommonUtil.getSqlBy_par(SqlFile.USER_DELETE_BYID, user_name));
	}

	@Override
	public int getTotalSize(String sql) {
		// TODO Auto-generated method stub
		ResultSet rs = DB.plsqlExec_res(sql);
		int count = 0;
		try {
			if(rs.next()){
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		return count;
	}

	@Override
	public User getUserByID(String id) {
		// TODO Auto-generated method stub
		ResultSet rs = DB.plsqlExec_res(CommonUtil.getSqlBy_par(SqlFile.USER_SELECT_BYID, id));
		User user = null;
		List<User> list = CommonUtil.getUserList_ByRs(rs);
		if(list.size()>0){
			user = list.get(0);
		}
		return user;
	}

	@Override
	public User getUserByName(String name) {
		// TODO Auto-generated method stub
		
		ResultSet rs = DB.plsqlExec_res(CommonUtil.getSqlBy_par(SqlFile.USER_SELECT_BYNAME, name));
		User user = null;
		List<User> list = CommonUtil.getUserList_ByRs(rs);
		if(list.size()>0){
			user = list.get(0);
		}
		return user;
	}

	@Override
	public List getUserList(int currentPage) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.USER_LIST, currentPage));
	}

	@Override
	public List getUserList_BySql(String sql) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(sql);
	}

	@Override
	public boolean saveUser(User user) {
		// TODO Auto-generated method stub
		boolean flag = DB.sqlNonExeQuery(CommonUtil.getSqlBy_par(SqlFile.USER_SAVE, user.getUser_name(),
				user.getUser_sex()==null?"":user.getUser_sex(),
				user.getUser_psw()==null?"":user.getUser_psw(),
				user.getUser_rename()==null?"":user.getUser_rename(),
				user.getUser_comppany()==null?"":user.getUser_comppany(),
				user.getUser_yue()==null?"":user.getUser_yue(),
				user.getUser_freeze()==null?"":user.getUser_freeze(),
				user.getUser_type()==null?"":user.getUser_type(),
				user.getUser_phone()==null?"":user.getUser_phone(),
				user.getUser_duty()==null?"":user.getUser_duty(),
				user.getUser_telphone()==null?"":user.getUser_telphone(),
				user.getUser_mail()==null?"":user.getUser_mail(),
				user.getUser_weibo_path()==null?"":user.getUser_weibo_path(),
				user.getUser_duty_sort()==null?"":user.getUser_duty_sort(),
				user.getUser_city()==null?"":user.getUser_city()==null,
				user.getUser_order_sort()==null?"":user.getUser_order_sort(),
				user.getUser_order_time()==null?"":user.getUser_order_time(),
				user.getUser_state()==null?"":user.getUser_state(),
				user.getUser_exit_time()==null?"":user.getUser_exit_time(),
				user.getUser_start_time()==null?"":user.getUser_start_time(),
				user.getUser_end_time()==null?"":user.getUser_end_time()));
		return flag;
	}

	@Override
	public boolean updateUser(User user) {
		// TODO Auto-generated method stub
		boolean flag = DB.sqlNonExeQuery(CommonUtil.getSqlBy_par(SqlFile.USER_UPDATE, user.getUser_name(),
				user.getUser_sex()==null?"":user.getUser_sex(),
				user.getUser_psw()==null?"":user.getUser_psw(),
				user.getUser_rename()==null?"":user.getUser_rename(),
				user.getUser_comppany()==null?"":user.getUser_comppany(),
				user.getUser_yue()==null?"":user.getUser_yue(),
				user.getUser_freeze()==null?"":user.getUser_freeze(),
				user.getUser_type()==null?"":user.getUser_type(),
				user.getUser_phone()==null?"":user.getUser_phone(),
				user.getUser_duty()==null?"":user.getUser_duty(),
				user.getUser_telphone()==null?"":user.getUser_telphone(),
				user.getUser_mail()==null?"":user.getUser_mail(),
				user.getUser_weibo_path()==null?"":user.getUser_weibo_path(),
				user.getUser_duty_sort()==null?"":user.getUser_duty_sort(),
				user.getUser_city()==null?"":user.getUser_city()==null,
				user.getUser_order_sort()==null?"":user.getUser_order_sort(),
				user.getUser_order_time()==null?"":user.getUser_order_time(),
				user.getUser_state()==null?"":user.getUser_state(),
				user.getUser_exit_time()==null?"":user.getUser_exit_time(),
				user.getUser_start_time()==null?"":user.getUser_start_time(),
				user.getUser_end_time()==null?"":user.getUser_end_time(),
				user.getUser_id()==null?"":user.getUser_id()));
		return flag;
	}

	@Override
	public boolean addChargeBilling(String userid, int user_yue) {
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date=dateformat1.format(new Date());
		boolean flag = DB.sqlNonExeQuery(CommonUtil.getSqlBy_par(SqlFile.USER_CHARGE_SAVE,date,user_yue+"",userid));
		return flag;
	}

	@Override
	public List getBillingList(int currentPage) {
		List list=new ArrayList();
		List listDB=DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.BILLING_LIST, currentPage));
		if(listDB != null && listDB.size() > 0){
			Billing billing=null;
			for(int i=0;i<listDB.size();i++){
				billing=new Billing();
				String user_comppany=(String)((Map)listDB.get(i)).get("USER_COMPPANY");
				String user_rename=(String)((Map)listDB.get(i)).get("USER_NAME");
				Object user_yue=((Map)listDB.get(i)).get("USER_YUE");
				Object user_freeze=((Map)listDB.get(i)).get("USER_FREEZE");
				
				user_yue=user_yue==null?"0":user_yue;
				user_freeze=user_freeze==null?"0":user_freeze;
				
				billing.setName(user_rename);
				billing.setFreezeMoney(user_freeze+"");
				billing.setYue(user_yue+"");
				billing.setCompany(user_comppany);
				billing.setShiyongedu("0");
				int yue=Integer.parseInt(user_yue+"")-Integer.parseInt(user_freeze+"");
				billing.setUserMoney(yue+"");
				list.add(billing);
			}
		}else{
			list=null;
		}
		return list;
	}

	@Override
	public List getChargeRecordList(int currentPage) {
		List list=new ArrayList();
		List listDB=DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.CHARGE_RECORD_LIST, currentPage));
		if(listDB != null && listDB.size() > 0){
			Billing billing=null;
			for(int i=0;i<listDB.size();i++){
				billing=new Billing();
				Object date=((Map)listDB.get(i)).get("CHL_DATE");
				Object value=((Map)listDB.get(i)).get("CHL_VLALUE");
				String name=(String)((Map)listDB.get(i)).get("CHL_NAME");
				
				billing.setName(name);
				billing.setDate(date+"");
				billing.setValue(value+"");
				list.add(billing);
			}
		}else{
			list=null;
		}
		return list;
	}
	
	
}
